﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Spreadsheet;
using DevExpress.Spreadsheet.Export;
using DevExpress.Web;
using TheHeGeo.Util;
using TheHeGeoDao;

namespace TheHeGeo.Admin.AdminUc
{
    public partial class UcAdminSensorManager : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session[SessionConst.SES_GRV_SENSOR_ADMIN] = null;
                LoadData();
            }
            else
            {
                grvData.DataSource = Session[SessionConst.SES_GRV_SENSOR_ADMIN];
            }
        }

        protected void LoadData()
        {
            var data = DaoHelper.Entities.Sensors.ToList();
            Session[SessionConst.SES_GRV_SENSOR_ADMIN] = data;
            grvData.DataSource = data;
            grvData.DataBind();
        }

        protected bool CheckCongThuc(string congthuc)
        {
            var dt = new DataTable();
            try
            {
                dt.Compute(congthuc.Replace("x","1"), string.Empty);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        
        protected void grvData_RowDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e)
        {
            try
            {
                var str = (string) e.Values["MaSensor"];
                var lss = DaoHelper.Entities.Sensors.FirstOrDefault(p => p.MaSensor == str);
                if (lss != null)
                {
                    DaoHelper.Entities.Sensors.Remove(lss);
                    DaoHelper.Entities.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.StackTrace);
            }
            e.Cancel = true;
            grvData.CancelEdit();
            LoadData();
        }

        protected void grvData_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
        {
//            try
//            {
//                var lss = new Se();
//                lss.MaLoaiSensor = (string) e.NewValues["MaLoaiSensor"];
//                lss.TenLoaiSensor = (string) e.NewValues["TenLoaiSensor"];
//                lss.MoTa = (string) e.NewValues["MoTa"];
//                lss.CongThuc = (string) e.NewValues["CongThuc"];
//                lss.LoaiBieuDo = Convert.ToInt32(e.NewValues["LoaiBieuDo"]);
//                DaoHelper.Entities.LoaiSensors.Add(lss);
//                DaoHelper.Entities.SaveChanges();
//            }
//            catch (Exception ex)
//            {
//                throw new Exception(ex.StackTrace);
//            }
//            e.Cancel = true;
//            grvData.CancelEdit();
//            LoadData();
        }

        protected void grvData_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
        {
            try
            {
                var str = (string)e.NewValues["MaSensor"];
                var lss = DaoHelper.Entities.Sensors.FirstOrDefault(p => p.MaSensor == str);
                if(lss==null) throw new ArgumentException("Mã sensor không tồn tại");
                lss.CongThuc = (string)e.NewValues["CongThuc"];
                lss.DonVi = (string) e.NewValues["DonVi"];
                DaoHelper.Entities.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.StackTrace);
            }
            e.Cancel = true;
            grvData.CancelEdit();
            LoadData();
        }

        protected void grvData_ParseValue(object sender, DevExpress.Web.Data.ASPxParseValueEventArgs e)
        {
            if (e.FieldName=="CongThuc"&&!CheckCongThuc((string)e.Value))
            {
                throw new ArgumentException("Công thức không đúng định dạng");
            }

        }

        protected void grvData_InitNewRow(object sender, DevExpress.Web.Data.ASPxDataInitNewRowEventArgs e)
        {
        }

    }
}